/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p1gu;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Michael
 */
public class Menu {

    public static enum MenuState {

        MENU, OPTION, QUITTER
    }
    private ArrayList<String> listMenu;
    private int menuSelectionner;
    private Animation souris;
    private Framework framework;

    public Menu(Framework framework) {
        this.framework=framework;
        Thread threadInitialiseMenu = new Thread() {
            @Override
            public void run() {
                // Sets variables and objects for the game.
                Initialize();
                // Load game files (images, sounds, ...)
                LoadContent();
                Framework.gameState = Framework.GameState.MAIN_MENU;
            }
        };
        threadInitialiseMenu.start();
    }

    /**
     * Set variables and objects for the game.
     */
    private void Initialize() {
        menuSelectionner = 0;
        listMenu = new ArrayList<>();
        listMenu.add("Jouer");
        listMenu.add("Option");
        listMenu.add("Quitter");
        try {
            souris = new Animation(ImageIO.read(getClass().getResource("resources/images/souris.png")), 32, 32, 12, 150, true, 0, 0, 0);
           
        } catch (IOException ex) {
            Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Load game files - images, sounds, ...
     */
    private void LoadContent() {
    }

    /**
     * Update menu logic.
     *
     * @param gameTime gameTime of the game.
     * @param mousePosition current mouse position.
     */
    public void UpdateMenu(long gameTime, Point mousePosition) {
    }

    /**
     * Draw the menu to the screen.
     *
     * @param g2d Graphics2D
     * @param mousePosition current mouse position.
     */
    public void Draw(Graphics2D g2d, Point mousePosition) {
        int decalage = 20;
        for (int i = 0; i < listMenu.size(); i++) {
            if (menuSelectionner == i) {
                g2d.setColor(Color.red);
            } else {
                g2d.setColor(Color.blue);
            }
            g2d.drawString(listMenu.get(i), 10, decalage);
            decalage += 20;
        }
        g2d.setColor(Color.GREEN);
        drawSouris(g2d, mousePosition);

    }

    private void drawSouris(Graphics2D g2d, Point mousePosition) {
        if (mousePosition != null) {
            souris.changeCoordinates(mousePosition.x - 16, mousePosition.y - 16);
        }
        souris.Draw(g2d);
    }

    void keyReleadedFramework(KeyEvent e) {
        switch (e.getKeyCode()) {
            case 38:
                //haut
                if (menuSelectionner > 0) {
                    menuSelectionner--;
                } else {
                    menuSelectionner = listMenu.size() - 1;
                }
                break;
            case 40:
                if (menuSelectionner < listMenu.size() - 1) {
                    menuSelectionner++;
                } else {
                    menuSelectionner = 0;
                }
                break;
            case 10:
                switch (menuSelectionner) {
                    case 0:
                        framework.newGame();
                        break;
                    case 2:
                        System.exit(0);
                        break;
                    default:
                        break;
                }
                break;
            default:
                System.out.println(e.getKeyCode());
                break;

        }

    }

}
